﻿using BaseBll;
using BaseWebApi.App_Start;
using Common;
using System.Data;
using System.Net.Http;
using System.Web.Http;

namespace BaseWebApi.Controllers
{
    /// <summary>
    /// 系统日志
    /// </summary>
    [ControllerGroup(groupName: "SysLog", useage: "系统设置-系统日志")]
    public class SysLogController : BaseController
    {
        /// <summary>
        /// 日志查询
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页尺寸</param>
        /// <param name="fromDate">开始日</param>
        /// <param name="toDate">截至日</param>
        /// <param name="module">系统模块</param>
        /// <param name="method">操作类型</param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetSysLog(int pageIndex, int pageSize, string fromDate = "", string toDate = "", string module = "", string method = "")
        {
            string strWhere = "";
            if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
            {
                strWhere += " Where LogDate between '" + fromDate + " 00:00:00' and '" + toDate + " 23:59:59'";
            }
            else if (!string.IsNullOrEmpty(fromDate))
            {
                strWhere += " Where LogDate >= '" + fromDate + " 00:00:00'";
            }
            else if (!string.IsNullOrEmpty(toDate))
            {
                strWhere += " Where LogDate <= '" + toDate + " 23:59:59'";
            }
            if (!string.IsNullOrEmpty(module))
            {
                if (string.IsNullOrEmpty(strWhere))
                {
                    strWhere += " Where Module = '" + module + "'";
                }
                else
                {
                    strWhere += " and Module = '" + module + "'";
                }
            }
            if (!string.IsNullOrEmpty(method))
            {
                if (string.IsNullOrEmpty(strWhere))
                {
                    strWhere += " Where Method = '" + method + "'";
                }
                else
                {
                    strWhere += " and Method='" + method + "'";
                }
            }

            DataTable dt = SysLogBll.Instance.GetSysLog(pageSize, pageIndex, strWhere, "LogDate DESC", out int records);
            int totalPages = PageHelper.GetPageCount(pageSize, records);
            return JsonHelper.SuccessJson(CommonOperation.DataTable2JArray(dt), pageIndex, totalPages, records);

        }

    }
}
